package com.validation;


import com.validation.validator.*;

/**
 * 数据校验工具
 */
public class Validation {

    private Object value;

    private Boolean isAllow;

    private Validation() {
        this.isAllow = true;
    }

    public static Validation build() {
        return new Validation();
    }

    public Validation accept(Object value) {
        this.isAllow = true;
        this.value = value;
        return this;
    }

    /**
     * 满足条件才会校验
     *
     * @param condition
     * @param value
     * @return
     */
    public Validation ifTrueToAccept(Boolean condition, Object value) {
        this.isAllow = condition;
        this.value = value;
        return this;
    }

    public Validation isTrue(Boolean value, String message) {
        if (isAllow) {
            new TrueValidator().validate(value, message);
        }
        return this;
    }

    public Validation isFalse(Boolean value, String message) {
        if (isAllow) {
            new TrueValidator().validate(value, message);
        }
        return this;
    }

    public Validation notNull(String message) {
        if (isAllow) {
            new NotNullValidator().validate(this.value, message);
        }
        return this;
    }

    public Validation notEmpty(String message) {
        if (isAllow) {
            new NotEmptyValidator().validate(this.value, message);
        }
        return this;
    }

    public Validation regex(String regex, String message) {
        if (isAllow) {
            new RegexValidator().validate(regex, this.value, message);
        }
        return this;
    }


    public Validation max(Number max, String msg) {
        if (isAllow) {
            new MaxValidator().validate(max, this.value, msg);
        }
        return this;
    }


    public Validation min(Number min, String msg) {
        if (isAllow) {
            new MaxValidator().validate(min, this.value, msg);
        }
        return this;
    }


}
